package com.alks.common.utils.number;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.DecimalFormat;

/**
 * @author MS
 * @since 2024/11/27下午5:43
 */
public class AmtUtils {
    /**
     * 转为大写金额
     * @param n 小写金额
     * @return 大写金额
     */
    public static String digitCapital(double n) {
        String[] fraction = {"角", "分"};
        String[] digit = {"零", "壹", "贰", "叁", "肆", "伍", "陆", "柒", "捌", "玖"};
        String[][] unit = {{"元", "万", "亿"}, {"", "拾", "佰", "仟"}};

        String head = n < 0 ? "负" : "";
        // 如果是负数取绝对值
        n = Math.abs(n);
        String s = "";
        BigDecimal bigDecimal = new BigDecimal(Double.valueOf(n).toString());
        String nStr = bigDecimal.toString();
        // 小数部分
        String[] split = nStr.split("\\.");
        if (split.length > 1) {
            // 小数点为特殊符号，在分割时需进行转义
            String decimalStr = split[1];
            if (decimalStr.length() > 2) {
                decimalStr = decimalStr.substring(0, 2);
            }
            // 将小数部分转换为整数
            int integer = Integer.parseInt(decimalStr);
            String p = "";
            for (int i = 0; i < decimalStr.length() && i < fraction.length; i++) {
                p = digit[integer % 10] + fraction[decimalStr.length() - i - 1] + p;
                integer = integer / 10;
            }
            s = p.replaceAll("(零.)+", "") + s;
        }
        if (s.isEmpty()) {
            s = "整";
        }
        int integerPart = (int)Math.floor(n);
        // 整数部分
        for (int i = 0; i < unit[0].length && integerPart > 0; i++) {
            String p = "";
            for (int j = 0; j < unit[1].length && n > 0; j++) {
                p = digit[integerPart % 10] + unit[1][j] + p;
                integerPart = integerPart / 10;
            }
            s = p.replaceAll("(零.)*零$", "").replaceAll("^$", "零") + unit[0][i] + s;
        }
        return head + s.replaceAll("(零.)*零元", "元").replaceFirst("(零.)+", "").replaceAll("(零.)+", "零").replaceAll("^整$", "零元整");
    }

    /**
     * 万以上金额数据美化
     * @param number 待处理数据
     * @param keepTwoDecimals 是否保留两位小数
     * @return 例如百万级数据展示为x,xxx,xxx(百万)
     */
    public static String formatWithUnit(BigDecimal number, boolean keepTwoDecimals) {
        if (number == null) {
            return "0";
        }
        BigDecimal threshold = BigDecimal.valueOf(100000);
        if (number.abs().compareTo(threshold)>0) {
            String[] units = {"十万", "百万", "千万", "亿", "十亿", "百亿", "千亿"};
            int unitIndex = 0;
            BigDecimal number2 = number.abs().divide(threshold, 0, RoundingMode.HALF_UP);
            while (number2.compareTo(BigDecimal.TEN) >= 0 && unitIndex < units.length - 1) {
                number2 = number2.divide(BigDecimal.TEN, 0, RoundingMode.HALF_UP);
                unitIndex++;
            }
            /*截取判断*/
            DecimalFormat decimalFormat = new DecimalFormat();
            decimalFormat.setGroupingSize(3);
            decimalFormat.setGroupingUsed(true);
            if (keepTwoDecimals) {
                decimalFormat.setMaximumFractionDigits(2);
                decimalFormat.setMinimumFractionDigits(2);
            } else {
                decimalFormat.setMaximumFractionDigits(Integer.MAX_VALUE);
            }
            String formattedNumber = decimalFormat.format(number);
            return formattedNumber + "("+units[unitIndex]+")";
        }else{
            if (keepTwoDecimals){
                String pattern ="#.##";
                DecimalFormat decimalFormat = new DecimalFormat(pattern);
                return decimalFormat.format(number);
            }
            return number.toString();
        }
    }
}
